package org.srtc.infopub.dataReceiver;

import java.text.DecimalFormat;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.srtc.infopub.datacenter.storage.TestCache;

public class Consumer
	{

		private static String brokerURL = "vm://localhost";
		private static transient ConnectionFactory factory;
		private transient Connection connection;
		private transient Session session;

		public Consumer() throws JMSException
			{
				factory = new ActiveMQConnectionFactory(brokerURL);
				connection = factory.createConnection();
				connection.start();
				session = connection.createSession(false,
						Session.AUTO_ACKNOWLEDGE);
			}

		public void close() throws JMSException
			{
				if (connection != null)
					{
						connection.close();
					}
			}

		public static void main(String[] args) throws JMSException
			{
				Consumer consumer = new Consumer();
				for (String stock : args)
					{
						Destination destination = consumer.getSession()
								.createTopic("STOCKS." + stock);
						MessageConsumer messageConsumer = consumer.getSession()
								.createConsumer(destination);
						messageConsumer.setMessageListener(new Listener());
						while (true)
							{
								messageConsumer.receive();
								System.out.print("hell");
							}
					}
			}

	public static void start() throws JMSException {
		TestCache tc = TestCache.instance();

		Consumer consumer = new Consumer();

		Destination destination = consumer.getSession()
				.createTopic("WORK_DATA");
		MessageConsumer messageConsumer = consumer.getSession().createConsumer(
				destination);
		// messageConsumer.setMessageListener(new Listener());

		while (true) {
			try {
				MapMessage map = (MapMessage) messageConsumer.receive();
				tc.insertCache(map);
				System.out.println("got message");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

		public Session getSession()
			{
				return session;
			}

	}
